package com.tjetc.hw;

import java.util.Scanner;

public class 矩阵路径 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int m = scanner.nextInt();
        int[][] arr = new int[n][m];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                arr[i][j] = scanner.nextInt();
                if (i == 0) {
                    if (j == 0) {
                        continue;
                    }
                    // 第一行：只能从左来
                    arr[i][j] += arr[i][j - 1];
                } else if (j == 0) {
                    if (i== 0) {
                        continue;
                    }
                    // 第一列：只能从上来
                    arr[i][j] += arr[i - 1][j];}
                else {
                arr[i][j]=Math.min(arr[i][j]+arr[i-1][j],arr[i][j]+arr[i][j-1]);}
            }
        }
        System.out.println(arr[n-1][m-1]);

    }
}
